﻿using CommunityToolkit.Maui;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using MauiTurntable.dbModel;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MauiTurntable.ViewModel
{
    /// <summary>
    /// 编辑项目
    /// </summary>
    public partial class cvInfoEditModel : ObservableObject
    {
        /// <summary>
        /// 值
        /// </summary>
        [ObservableProperty]
        private int value;
        /// <summary>
        /// 名称
        /// </summary>
        [ObservableProperty]
        private string name;
        /// <summary>
        /// 组
        /// </summary>
        [ObservableProperty]
        private string group;
        /// <summary>
        /// 数据
        /// </summary>
        [ObservableProperty]
        private ObservableCollection<cvInfoEditModelItem> items;
        /// <summary>
        /// 保存
        /// </summary>
        public ICommand SaveCommand { get; set; }

        /// <summary>
        /// 数据访问
        /// </summary>
        private DalItem dal { get; set; }
        public event Action<string> Alarm;
        public cvInfoEditModel()
        {
            SaveCommand = new AsyncRelayCommand(SaveCommandMethod);

            items = new ObservableCollection<cvInfoEditModelItem>();
            dal = new DalItem();
            SelectData();
        }



        private async Task SelectData()
        {
            items.Clear();
            var alldata = await dal.SelectAllData();
            foreach (var item in alldata)
            {
                var additem = new cvInfoEditModelItem()
                {
                    GroupName = item.groupName,
                    Name = item.name,
                    Value = item.value,
                    CreateTimeLong = item.createTime,
                    dal = dal,
                };
                additem.Reash += Additem_Reash;
                additem.SetTime();
                items.Add(additem);
            }
        }

        private void Additem_Reash()
        {
            SelectData();
        }

        private async Task SaveCommandMethod()
        {
            if (string.IsNullOrEmpty(group))
            {
                Alarm?.Invoke("组名称不能为空");
                return;
            }
            if (string.IsNullOrEmpty(name))
            {
                Alarm?.Invoke("名称不能为空");
                return;
            }
            if (value <= 0)
            {
                Alarm?.Invoke("权重需要大于0");
                return;
            }
            DBItem data = new DBItem()
            {
                createTime = DateTime.Now.ToFileTime(),
                groupName = group,
                name = name,
                value = value
            };
            await dal.AddData(data);
            await SelectData();
        }
    }
    /// <summary>
    /// 
    /// </summary>
    public partial class cvInfoEditModelItem : ObservableObject
    {
        /// <summary>
        /// 创建时间
        /// </summary>
        [ObservableProperty]
        private long createTimeLong;

        /// <summary>
        /// 组名称
        /// </summary>
        [ObservableProperty]
        private string groupName;

        /// <summary>
        /// 名称
        /// </summary>
        [ObservableProperty]
        private string name;

        /// <summary>
        /// 权重
        /// </summary>
        [ObservableProperty]
        private int value;
        /// <summary>
        /// 
        /// </summary>
        [ObservableProperty]
        private DateTime _createTime;

        public ICommand DelCommand { get; set; }
        /// <summary>
        /// 数据访问
        /// </summary>
        public DalItem dal { get; set; }

        public event Action Reash;
        public cvInfoEditModelItem()
        {
            DelCommand = new AsyncRelayCommand(DelCommandMethod);
        }
        private async Task DelCommandMethod()
        {
            await dal.DelectData(createTimeLong);
            Reash?.Invoke();
        }
        public void SetTime()
        {
            _createTime = DateTime.FromFileTime(createTimeLong);
        }
    }
}
